package tests;

/**
 * @author 李夏
 * @version 1.0
 */

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.lang.reflect.Method;
import java.time.Duration;

/**
 * 帖子类测试
 */
public class ArticlePage extends Utils {
    public static String url = "http://127.0.0.1:58080/sign-in.html";
    public ArticlePage() {
        super(url);
    }

    /*
    登录后，点击首页显示所有帖子，点击其他模块，应显示对应模块下的帖子
     */
    public void  articlePageRight() throws InterruptedException, IOException {
        LoginPage loginPage=new LoginPage();
        loginPage.loginSuc();
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(2) > a > span.nav-link-title")).click();
        driver.findElement(By.cssSelector("#article_list_board_title"));

        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);


    }
    /*
    编辑帖子页面能够正常打开，正常填写（标题内容都要填写）帖子能提交成功
     */
    public void articlePubSuc() throws IOException {
        LoginPage loginPage=new LoginPage();
        loginPage.loginSuc();
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"));
       driver.findElement(By.cssSelector("#article_post_title")).sendKeys("请给我一个offer");
        driver.findElement(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll")).sendKeys("哈喽哈喽");
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
    }

    /*
    不写标题、不写内容、都不写，异常填写提交失败
     */
    public void articlePubFail() throws IOException {
        LoginPage loginPage=new LoginPage();
        loginPage.loginSuc();
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"));
        driver.findElement(By.cssSelector("#article_post_title")).sendKeys("请给我一个offer");
        driver.findElement(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll")).sendKeys("哈喽哈喽");
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"));
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
    }
    /*
    点赞帖子，该帖子状态正常，点赞成功
     */
    public void articleLikes() throws IOException {
        LoginPage loginPage=new LoginPage();
        loginPage.loginSuc();
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        driver.findElement(By.cssSelector("#details_btn_like_count")).click();
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
    }
    /*
    点击删除按钮会出现弹窗，点击弹窗右上角的叉或者左下角的取消则取消当前删除操作，点击右下角删除则删除当前帖子，删除成功
     */
    public void articleDelSuc() throws IOException, InterruptedException {
        LoginPage loginPage=new LoginPage();
        loginPage.loginSuc();
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        // 创建 WebDriverWait 实例，设置最长等待时间为 10 秒
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        // 定位删除按钮并点击
        WebElement deleteButton = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a > span")));
        deleteButton.click();
        // 等待弹窗出现
        WebElement confirmationPopup = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#details_delete_modal > div > div > div.modal-body.text-center.py-4")));
        // 这里可以根据需要选择取消或删除操作

        // 点击右上角叉取消删除
        // WebElement closeCross = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#confirmation-popup-id .close-cross")));
        // closeCross.click();

        // 点击左下角取消按钮取消删除
        // WebElement cancelButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("cancel-button-id")));
        // cancelButton.click();

        // 点击右下角删除按钮进行删除操作
        driver.findElement(By.cssSelector("#details_artile_delete")).click();
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
    }
    /*
    该帖子状态正常，评价不为为空，发布成功
     */
    public void articleAckSuc() throws IOException {
        LoginPage loginPage=new LoginPage();
        loginPage.loginSuc();
         driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
         driver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll")).sendKeys("w喂喂喂");
         driver.findElement(By.cssSelector("#nav_board_index > a > span.nav-link-title"));
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
    }
    /*
    该帖子状态正常，评价为空，发布失败，给出错误提示
     */
    public void articleAckFail() throws IOException {
        LoginPage loginPage=new LoginPage();
        loginPage.loginSuc();
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        driver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll")).sendKeys("w喂喂喂");
        String text = driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2) > svg")).getText();
        assert text.equals("请输入回复内容");
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);

    }


}
